
const { Order } = require("../models/orderModel");
const { User } = require("../models/userModel")
const { Car } = require('../models/carModel')
//新增订单
async function addOrder(req, res) {
    try {
        // console.log("REQ___BODY___!!!!", req.body);
        // spentTime  租车的天数
        const { userID, carID, ownerID, spentMoney, startTime, endTime, spentTime } = req.body;
        // console.log('ssss', carID);
        // status:'requesting'  求租状态
        const data = await Order.create({ userID, carID, spentMoney, startTime, endTime, spentTime, status: 'requesting' })
        const gotOrderID = data._id
        //数据关联 格式为： 模型.findByIdAndUpdate(查询条件,{$push:{key:value}})
        await User.findByIdAndUpdate(userID, { $push: { customerOrders: gotOrderID } })
        await User.findByIdAndUpdate(ownerID, { $push: { ownerOrders: gotOrderID } })
        // await User.findByIdAndUpdate()
        // await User.findOneAndUpdate(carID, { $push: { cars: carID } })
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '新增成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '新增失败',
        })
    }


}
//删除订单
async function delOrder(req, res) {
    try {
        const { _id } = req.body;
        const data = await Order.findByIdAndDelete(_id)
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '删除订单成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '删除订单失败'
        })
    }
}

//通过id查找一个订单
async function findOrderByID(req, res) {
    try {
        const { _id } = req.body;
        console.log('id', _id);
        const data = await Order
            .findById(_id)
            .populate('userID')
            .populate('carID');
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '查找成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '查找失败'
        })
    }
}
// 通过用户id查找订单
async function findOrderByUser(req, res) {
    try {
        const { userID } = req.body;
        console.log('id', userID);
        const data = await Order
            .find(userID)
            .populate('userID')
            .populate('carID');
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '查找成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '查找失败'
        })
    }
}
//修改订单的信息
async function changeOrderInfo(req, res) {
    try {
        const { _id, startTime, endTime, spentTime } = req.body;
        const data = await Order.updateOne({ _id }, { startTime, endTime, spentTime })
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '修改订单的信息成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '修改订单的信息失败'
        })
    }
}

//搜索订单信息
async function findAllOrder(req, res) {
    try {
        const { pageSize, currentPage, type = {}, value = {} } = req.body;
        //总条数
        const total = await Order.countDocuments();
        //总页数
        const totalPages = Math.ceil(total / pageSize);
        const data = await Order
            .find(
                {
                    $and:
                        [
                            { [type]: { $regex: value, $options: '$i' } }//模糊查询
                        ]
                }
            )
            .limit(pageSize - 0)//设置请求条数
            .skip((currentPage - 1) * pageSize)//设置跳过条数
            .populate('userID')
            .populate('carID');
        if (data) {
            res.send({
                status: 1,
                data: { total, totalPages, data, pageSize, currentPage },
                msg: "查找成功"
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: "查找失败"
        })
    }
}
//查找所有的订单
async function findOrder(req, res) {
    try {
        const { pageSize, currentPage } = req.body;
        console.log('ffffff');
        //总条数
        const total = await Order.countDocuments();
        //总页数
        const totalPages = Math.ceil(total / pageSize);
        const data = await Order
            .find()
            .limit(pageSize - 0)//设置请求条数
            .skip((currentPage - 1) * pageSize)//设置跳过条数
            .populate('userID')
            .populate('carID');
        if (data) {
            res.send({
                status: 1,
                data: { total, totalPages, data, pageSize, currentPage },
                msg: "查找成功"
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: "查找失败"
        })
    }
}

//修改订单的状态
async function changeOrderStatus(req, res) {
    try {

        const { _id, status } = req.body;
        console.log('修改成功没有',_id,status);
        const data = await Order.findByIdAndUpdate({ _id }, { status });
      
        if (data) {
            res.send({
                status: 1,
                data,
                msg: '修改订单状态成功'
            })
        }
    } catch (error) {
        console.log('error', error);
        res.send({
            status: 0,
            msg: '修改订单状态失败'
        })
    }
}


module.exports = { findOrder, addOrder, delOrder, changeOrderInfo, findOrderByUser, findOrderByID, findAllOrder, changeOrderStatus }